package com.googlecode.lookie.swing.laf;

import java.util.ArrayList;
import java.util.List;

public class LookAndFeelRegistry
{

    private List<LookAndFeelDescriptor> descriptors = new ArrayList<LookAndFeelDescriptor>();

    public void addDescriptor( LookAndFeelDescriptor descriptor )
    {
        descriptors.add( descriptor );
    }

    public void removeDescriptor( LookAndFeelDescriptor descriptor )
    {
        descriptors.remove( descriptor );
    }

    public void contribute( LookAndFeelDescriptorContributor contributor )
    {
        contributor.contribute( this );
    }

    public LookAndFeelDescriptor[] getDescriptors()
    {
        return descriptors.toArray( new LookAndFeelDescriptor[descriptors.size()] );
    }

    public LookAndFeelDescriptor[] getDescriptors( String groupId )
    {
        List<LookAndFeelDescriptor> list = new ArrayList<LookAndFeelDescriptor>();
        for ( LookAndFeelDescriptor d : descriptors )
        {
            if ( d.getGroupId().equals( groupId ) )
            {
                list.add( d );
            }
        }
        return list.toArray( new LookAndFeelDescriptor[list.size()] );
    }

    public LookAndFeelDescriptor getDescriptor( String id )
    {
        for ( LookAndFeelDescriptor d : descriptors )
        {
            if ( d.getId().equals( id ) )
            {
                return d;
            }
        }
        return null;
    }

    public String[] getGroupIds()
    {
        List<String> list = new ArrayList<String>();
        for ( LookAndFeelDescriptor d : descriptors )
        {
            String groupId = d.getGroupId();
            if ( !list.contains( groupId ) )
            {
                list.add( groupId );
            }
        }
        return list.toArray( new String[list.size()] );
    }

}
